/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-01-18
 * Time: 16:24
 */
public class Test {
    public static MySingleList.Node mergeTwoLists(MySingleList.Node head1,MySingleList.Node head2) {
        MySingleList.Node newHead = new MySingleList.Node(0);
        MySingleList.Node tmp = newHead;
        while(head1 != null && head2 != null) {
            if(head1.val < head2.val){
                tmp.next = head1;
                head1 = head1.next;
                tmp = tmp.next;
            }
            else {
                tmp.next = head2;
               head2 = head2.next;
                tmp = tmp.next;
            }
        }
        if(head1 != null) {
            tmp.next = head1;
        }
        if(head2 != null) {
            tmp.next = head2;
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        MySingleList mySingleList2 = new MySingleList();
        mySingleList1.addLast(2);
        mySingleList1.addLast(7);
        mySingleList1.addLast(34);
        mySingleList1.addLast(55);
        mySingleList2.addLast(21);
        mySingleList2.addLast(22);
        mySingleList2.addLast(56);
        mySingleList2.addLast(77);
        mySingleList1.display();
        mySingleList2.display();
        MySingleList.Node ret = mergeTwoLists(mySingleList1.head,mySingleList2.head);
        mySingleList1.display(ret);
    }

}

